#include <stdio.h>

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <SDL_thread.h>
#include "SDL.h"
#include <string>
#include "sdl_image.h"
#include "tileManager.h"
#include "Draw.h"
#include "Drawable.h"
#include "Player.h"
#include "Wall.h"
#include "floor.h"
#include "Levels.h"
#include "TextManager.h"
#include "Actor.h"
//The surfaces that will be used
SDL_Surface *message = NULL;
SDL_Surface *tileset = NULL;
SDL_Surface *screen = NULL; 

/* the Lua interpreter */
lua_State* L;

int main( int argc, char* args[] )
{	
	/* initialize Lua */
	//L = lua_open();

	/* load Lua base libraries */
	//luaL_openlibs(L);

	/* run the script */
	//luaL_dofile(L, "test.lua");

	/* cleanup Lua */
	//lua_close(L);
	 

	bool gameOver = false;
	IMG_INIT_PNG; 

	SDL_Init(SDL_INIT_VIDEO);
	if( TTF_Init() == -1 )
    {
        return 5;    
    }
	Drawable d =* new Drawable();
	//Initialize all SDL subsystems
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return 1;
	}

	//Set up the screen
	screen = SDL_SetVideoMode( TileManager::SCREEN_WIDTH, TileManager::SCREEN_HEIGHT, TileManager::SCREEN_BPP, SDL_SWSURFACE );

	//If there was an error in setting up the screen
	if( screen == NULL )
	{
		return 1;
	}
	Draw::screen = screen;
	//draw black to the screen before we begin
	//SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );

	//Set the window caption
	SDL_WM_SetCaption( "ROGUELIKE - THE ROGUELIKELIKE", NULL );
	 
	tileset = Draw::load_image("tileset.gif");
	if(tileset==NULL)
		return 1;

	//full power to the TileManager
	TileManager *tm = new TileManager(tileset, screen); 
	Player * player = new Player(tm);
	Actor * mon = new Actor(tm);
	Levels* levels  = new Levels(tm);
	levels->startLevel1();
	tm->spawn(player , 2,2);
	tm->spawn(mon, 5,5);
	SDL_Event event=*new SDL_Event();
	SDL_Color col = *new SDL_Color();
	col.b = 100;
	col.g = 100;
	col.r=100;
	
	//BEGIN GAME LOOP
	while(!gameOver){
		

	//	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );//"resets" the screen so we don't get weird behavior with alpha
		SDL_WaitEvent(&event);

		//If a key was pressed
		if( event.type == SDL_KEYDOWN )
		{ 

			switch( event.key.keysym.sym )
			{
				//need a class to store keys generically so user can set them,
				//or so that they can change during development
			case SDLK_UP:tm->move(player , 1, "N"); break;
			case SDLK_DOWN:tm->move(player , 1, "S"); break;
			case SDLK_LEFT:tm->move(player , 1, "W"); break;
			case SDLK_RIGHT:tm->move(player , 1, "E"); break;
			case SDLK_q:gameOver = true; break;
			case SDLK_u:TextManager::Message("u pressed");break;
			case SDLK_i:TextManager::Message("i pressed");break;
			default:break;  
			}
		}


		//If the user has Xed out the window
		else if( event.type == SDL_QUIT )
		{
			//Quit the program
			gameOver = true;
		}

		
	Draw::setColor(player->graphic, col);

		//Update the screen
		TextManager::update();
		tm->update();
	//	TextManager::Message("it works");
	//	Draw::putTextAt(50,850,"it works");
	//	TextManager::Message("testing");
	Draw::setColor(player->graphic, col);
		if( SDL_Flip( screen ) == -1 )
		{
			return 1;    
		}
	}//end of gameloop 

	//Free the surfaces
	SDL_FreeSurface( message );
	SDL_FreeSurface( tileset );

	//Quit SDL
	SDL_Quit();

	return 0;
}

